// services/connections.ts - 连接管理 API 服务
import apiClient from './api';
import type {
  ConnectionDetail,
  CreateConnectionRequest,
  ListResponse,
  ApiResponse,
  SystemInfo,
} from '../types/api';

/**
 * 获取所有连接列表
 */
export const listConnections = async (): Promise<ListResponse<ConnectionDetail>> => {
  const response = await apiClient.get<ListResponse<ConnectionDetail>>('/connections');
  return response.data;
};

/**
 * 获取单个连接详情
 * @param connection 连接ID
 */
export const getConnection = async (connection: string): Promise<ConnectionDetail> => {
  const response = await apiClient.get<ConnectionDetail>('/connection', {
    params: { connection },
  });
  return response.data;
};

/**
 * 创建新连接
 * @param connectionConfig 连接配置
 */
export const createConnection = async (
  connectionConfig: CreateConnectionRequest
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/connections', connectionConfig);
  return response.data;
};

/**
 * 删除连接
 * @param connection 连接ID
 */
export const deleteConnection = async (connection: string): Promise<ApiResponse> => {
  const response = await apiClient.delete<ApiResponse>('/connection', {
    params: { connection },
  });
  return response.data;
};

/**
 * 关闭连接
 * @param connection 连接ID
 */
export const closeConnection = async (connection: string): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/connection/close', null, {
    params: { connection },
  });
  return response.data;
};

/**
 * 重新连接
 * @param connection 连接ID
 */
export const reconnectConnection = async (connection: string): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/connection/reconnect', null, {
    params: { connection },
  });
  return response.data;
};

/**
 * 重新连接所有断开的连接
 */
export const reconnectAllConnections = async (): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/connections/reconnect-all');
  return response.data;
};

/**
 * 设置默认连接
 * @param connection 连接ID
 */
export const setDefaultConnection = async (connection: string): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/connection/set-default', null, {
    params: { connection },
  });
  return response.data;
};

/**
 * 连接健康检查
 * @param connection 连接ID
 */
export const checkConnectionHealth = async (
  connection: string
): Promise<ApiResponse> => {
  const response = await apiClient.get<ApiResponse>('/connection/health', {
    params: { connection },
  });
  return response.data;
};

/**
 * 获取连接信息
 * @param connection 连接ID
 */
export const getConnectionInfo = async (connection: string): Promise<ApiResponse> => {
  const response = await apiClient.get<ApiResponse>('/connection/info', {
    params: { connection },
  });
  return response.data;
};

/**
 * 获取连接能力（capabilities）
 * @param connection 连接ID
 */
export const getConnectionCapabilities = async (connection: string): Promise<string> => {
  const response = await apiClient.get<string>('/connection/capabilities', {
    params: { connection },
    headers: { Accept: 'text/xml' },
  });
  return response.data;
};

/**
 * 获取系统信息
 * @param connection 连接ID
 */
export const getSystemInfo = async (connection: string): Promise<SystemInfo> => {
  const response = await apiClient.get<SystemInfo>('/system/info', {
    params: { connection },
  });
  return response.data;
};

/**
 * 获取主机能力
 * @param connection 连接ID
 */
export const getSystemCapabilities = async (connection: string): Promise<string> => {
  const response = await apiClient.get<string>('/system/capabilities', {
    params: { connection },
    headers: { Accept: 'text/xml' },
  });
  return response.data;
};

// 导出所有服务
export const connectionService = {
  listConnections,
  getConnection,
  createConnection,
  deleteConnection,
  closeConnection,
  reconnectConnection,
  reconnectAllConnections,
  setDefaultConnection,
  checkConnectionHealth,
  getConnectionInfo,
  getConnectionCapabilities,
  getSystemInfo,
  getSystemCapabilities,
};